﻿package src.computation {
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import flash.utils.Dictionary;
	//
	import src.entity.Link;
	import src.entity.Node;	
	import src.entity.Element;
	
	public class CLink extends CEntity implements ICEntity{
		
		public var end:Dictionary; 		//dictionary contains id and type
		public var start:Dictionary;
		public const type = Link.TYPE
		
		public function CLink(){			
		}
		
		public function getOtherEnd(inNode) {
			if (end != null && inNode.id == end["id"] && inNode.type == end["type"]) {
				if (start != null) {
					return start;
				} 
			}
			if (start != null && inNode.id == start["id"] && inNode.type == start["type"]) {
				if (end != null) {
					return end;
				} 
			}
			return null;
		}
		
		public function addEnd(b) {
			if (end == null && b!=null) {
				end = new Dictionary();
				end["id"] = b.id;
				end["type"] = b.type;
			}
			if (start == null && b!=null) {
				start = new Dictionary();
				start["id"] = b.id;
				start["type"] = b.type;
			}
		}
		
		public function removeEnd(b) {// b is node or elements			
			if (end!=null && end["id"] == b.id && end["type"] == b.type) {
				b.unLink(this.id);
				end = null;				
			}			
			if (start!=null && start["id"] == b.id && start["type"] == b.type) {
				b.unLink(this.id);
				start = null;				
			}
		}		
		
		public function unlinkElements(elements:Dictionary) {
			if (end!=null && elements!=null && elements[end["id"]] != null && end["type"] == Element.TYPE) {
				elements[end["id"]].unLink(this.id);
				end = null;
			}
			if (start!=null && elements!=null && elements[start["id"]]!=null && start["type"]== Element.TYPE) {				
				elements[start["id"]].unLink(this.id);
				start = null;
			}			
		}
		
		
		public function unlinkNode(nodes:Dictionary) {
			if (end!=null && nodes!=null && nodes[end["id"]] != null && end["type"] == Node.TYPE) {
				nodes[end["id"]].unLink(this.id);
				end = null;
			}
			if (start!=null && nodes!=null && nodes[start["id"]]!=null && start["type"]==Node.TYPE) {				
				nodes[start["id"]].unLink(this.id);
				start = null;
			}			
		}
		
		public function print():void {
			trace(this.type + "----------");
			this.printBase();
			if(this.end!=null){
				trace("id end:" + this.end["type"] + " "+this.end["id"]);
			}else {
				trace("id end: null");
			}
			if(this.start!=null){
				trace("id start:" + this.start["type"] + " "+this.start["id"]);
			}else {
				trace("id start: null");
			}			
			trace("----------------");
		}
		
		/**
		 * clone
		 * return copy of this object
		 */
		public override function clone() {			
			var cloneObj = new CLink();
			// common parametrs
			cloneObj.id = this.id;
			cloneObj.name = this.name;
			cloneObj.description = this.description;
			//
			var dict = new Dictionary();
			dict["id"] = end["id"];
			dict["type"] = end["type"];
			cloneObj.end = dict;
			var dict = new Dictionary();
			dict["id"] = start["id"];
			dict["type"] = start["type"];
			cloneObj.start = dict;
			return cloneObj;
		}
		
	}
	
}